<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="字典列表" width="600px">
    <BasicTable @register="registerTable" :onRowClick="handleClick" :onRowDbClick="handleDblclick" :rowClassName="rowClassName">
      <template #tableTitle>
        <a-button type="primary" color="success" @click="handleCreate"> 新增</a-button>
        <a-button type="primary" color="warning" @click="handleEdit"> 编辑</a-button>
        <PopConfirmButton 
          type="primary" 
          color="error" 
          title="是否确认删除？" 
          okText="确定" 
          cancelText="取消" 
          preIcon="ant-design:delete-twotone" 
          @confirm="handleDelete" 
        >删除
        </PopConfirmButton>
      </template>
      <!--
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      -->
    </BasicTable>
  </BasicDrawer>
  <DictItemModal @register="registerModal" @success="handleSuccess" :dictId="dictId" />
</template>
<script lang="ts" setup>
  import { ref, unref, reactive } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { PopConfirmButton } from '/@/components/Button';
  import { dictItemColumns } from '../dict.data';
  import { itemList, deleteItem } from '../dict.api';
  import { useMessage } from "/@/hooks/web/useMessage";

  import DictItemModal from './DictItemModal.vue';

  const $message = useMessage();
  const { prefixCls } = useDesign('row-invalid');
  const dictId = ref('');
  //字典配置model
  const [registerModal, { openModal }] = useModal();
  const [registerDrawer] = useDrawerInner(async (data) => {
    dictId.value = data.id;
    setProps({ searchInfo: { dictId: unref(dictId) } });
    reload();
  });

  /**
   * 表格配置
   */
  const [registerTable, { reload, setProps }] = useTable({
    api: itemList,
    columns: dictItemColumns,
    showIndexColumn: false,
    striped: true,
    useSearchForm: false,
    bordered: true,
    immediate: false,
    size: "small",
  });

  /**
   * 新增
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑
   */
  function handleEdit() {
    if (!selectRows.id) {
      $message.createMessage.warning('请选择一条数据');
      return
    }
    let record = {
      id : selectRows.id,
      dictId : selectRows.dictId,
      itemText : selectRows.itemText,
      itemValue : selectRows.itemValue,
      sortOrder : selectRows.sortOrder,
      tenantId : selectRows.tenantId,
      description : selectRows.description,
      status : selectRows.status,
      hidden : selectRows.hidden,
    }
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 删除
   */
  async function handleDelete() {
    if (!selectRows.id) {
      $message.createMessage.warning('请选择一条数据');
      return
    }
    await deleteItem({ id: selectRows.id }, reload);
  }

  //单击行
  let selectRows = reactive<any>({ id: '' });
  function handleClick(record) {
    if (selectRows.id != record.id) {
      Object.assign(selectRows, record);
    } else {
      // Object.keys(selectRows).forEach( key => {
      //   delete selectRows[key]
      // })
    }
  }

  //定义行样式
  function rowClassName(record, index) {
    let className = '';
    if (record.id === selectRows.id) {
      className = 'row-selected';
    }
    //return record.status == 0 ? prefixCls : '';
    return className;
  }

  function handleDblclick() {
    handleEdit();
  }

  /**
   * 
   * @param record 
   */
  function handleSuccess(data) {
    Object.assign(selectRows, data);
    reload();
  }
</script>
<style scoped lang="less">
  @prefix-cls: ~'@{namespace}-row-invalid';

  ::v-deep(.@{prefix-cls}) {
    background: #f4f4f4;
    color: #bababa;
  }
</style>
